package hackthon.core;

import hackthon.pojo.Employee;
import redis.clients.jedis.Jedis;

import java.io.IOException;

//import org.apache.log4j.Logger;



/**
 * Created by wuwenhao on 14/10/27.
 */
public class MyRedis {



    //notice:the serializable object should use the restore function, or the return value will not the origin value
    public void setObject(String key, Object value){
        Jedis jedis = MyRedisPool.getInstance();
        boolean broken = false;
        try {
            jedis.set(key.getBytes(),MySerializable.serializable(value));
        } catch (Exception e) {
            broken = true;
            e.printStackTrace();
        }finally {
           MyRedisPool.releaseSource(jedis,broken);
        }
    }

    public byte[] getObject(byte[] bytes){
        Jedis jedis = MyRedisPool.getInstance();
        boolean broken = false;
        byte b[] = null;
        try {
             b = jedis.get(bytes);
        } catch (Exception e) {
            broken = true;
            e.printStackTrace();
        }finally {
            MyRedisPool.releaseSource(jedis,broken);
        }
        return b;
    }

    public void setStr(String key, String value){
        Jedis jedis = MyRedisPool.getInstance();
        boolean broken = false;
        try {
            jedis.set(key, value);
        } catch (Exception e) {
            broken = true;
            e.printStackTrace();
        }finally {
            MyRedisPool.releaseSource(jedis,broken);
        }
    }

    public String getStr(String key){
        Jedis jedis = MyRedisPool.getInstance();
        boolean broken = false;
        String value = null;
        try {
            value = jedis.get(key);
        }catch (Exception e){
            broken = true;
            e.printStackTrace();
        }finally {
            MyRedisPool.releaseSource(jedis, broken);
        }
        return value;
    }

    public boolean existsKey(String key){
        Jedis jedis = MyRedisPool.getInstance();
        boolean existFlag = false;
        boolean broken = false;
        try {
            if(jedis.exists(key)){
                existFlag = true;
            }
        }catch (Exception e){
            broken = true;
            e.printStackTrace();;
        }finally {
            MyRedisPool.releaseSource(jedis, broken);
            return existFlag;
        }
    }

    public boolean existsKeyByte(byte[] bytes){
        Jedis jedis = MyRedisPool.getInstance();
        boolean existFlag = false;
        boolean broken = false;
        try {
            if(jedis.exists(bytes)){
                existFlag = true;
            }
        }catch (Exception e){
            broken = true;
            e.printStackTrace();;
        }finally {
            MyRedisPool.releaseSource(jedis, broken);
            return existFlag;
        }
    }
    
    public static void main(String[] args) {
    	/*Logger logger = Logger.getLogger("Test");

    	logger.info("hello log4j");*/
        MyRedis redis = new MyRedis();
//        Jedis jedis = new Jedis("127.0.0.1", 6379); //MyRedisPool.getInstance();
//        jedis.auth("wr61212");
    	Employee emp = new Employee();
    	//emp.setAge(13);
    	emp.setId(13);
    	emp.setName("xiaohaobi");
        redis.setObject("Test1", emp);
        //redis.setObject("Test2", emp);
        //redis.setObject("Test3", emp);

        byte[] test = redis.getObject("Test1".getBytes());

       // System.out.println("6.1: " + test);
        ///byte[] test1 = redis.get("Test1".getBytes());
        //System.out.println("6.2: " + test1);
        try {
			Employee emp1 = (Employee) MySerializable.unSerializable(test);
			System.out.println("6: " + emp1.getName() + "="  + "=" + emp1.getId());
		} catch (IOException e) {
			e.printStackTrace();
		}

    }
}
